﻿using System;

namespace BankSimulation
{
    // Account类模拟银行账户
    public class Account
    {
        // 字段
        private double balance;

        // 属性
        public string AccountHolder { get; set; } // 账户持有人
        public string AccountNumber { get; private set; } // 账户号码（只读）

        // 索引器
        public double this[int index]
        {
            get { return balance; }
            set { if (value >= 0) balance = value; }
        }

        // 构造函数
        public Account(string accountHolder, string accountNumber, double initialBalance = 0)
        {
            AccountHolder = accountHolder;
            AccountNumber = accountNumber;
            this.balance = initialBalance;
        }

        // 方法：存款
        public void Deposit(double amount)
        {
            if (amount > 0)
            {
                balance += amount;
                Console.WriteLine($"存款成功，账户余额：{balance}元");
            }
            else
            {
                Console.WriteLine("存款金额必须大于0");
            }
        }

        // 方法：取款
        public virtual void Withdraw(double amount)
        {
            if (amount <= balance)
            {
                balance -= amount;
                Console.WriteLine($"取款成功，账户余额：{balance}元");
            }
            else
            {
                Console.WriteLine("余额不足，取款失败");
            }
        }

        // 方法：查询余额
        public double GetBalance()
        {
            return balance;
        }
    }

    // CreditAccount类继承自Account，模拟信用账号
    public class CreditAccount : Account
    {
        // 字段
        private double creditLimit;

        // 属性
        public double CreditLimit
        {
            get { return creditLimit; }
            set { creditLimit = value > 0 ? value : 0; }
        }

        // 构造函数
        public CreditAccount(string accountHolder, string accountNumber, double initialBalance, double creditLimit)
            : base(accountHolder, accountNumber, initialBalance)
        {
            this.creditLimit = creditLimit;
        }

        // 重写取款方法：允许透支
        public override void Withdraw(double amount)
        {
            double availableBalance = GetBalance() + creditLimit;
            if (amount <= availableBalance)
            {
                double newBalance = GetBalance() - amount;
                if (newBalance < 0)
                {
                    creditLimit -= Math.Abs(newBalance);  // 如果账户余额不足，使用信用额度
                    base.Withdraw(GetBalance());  // 提取账户余额
                }
                else
                {
                    base.Withdraw(amount);  // 正常取款
                }

                Console.WriteLine($"取款成功，账户余额：{GetBalance()}元，信用额度剩余：{creditLimit}元");
            }
            else
            {
                Console.WriteLine("信用额度不足，取款失败");
            }
        }
    }

    // ATM类模拟自动取款机
    public class ATM
    {
        public void DepositToAccount(Account account, double amount)
        {
            account.Deposit(amount);
        }

        public void WithdrawFromAccount(Account account, double amount)
        {
            account.Withdraw(amount);
        }
    }

    // 主程序入口
    class Program
    {
        static void Main(string[] args)
        {
            // 创建一个普通账号
            Account account1 = new Account("张三", "123456789", 1000);
            Console.WriteLine($"账户1: {account1.AccountHolder}, 账户余额: {account1.GetBalance()}元");

            // 创建一个信用账号
            CreditAccount creditAccount = new CreditAccount("李四", "987654321", 500, 1000);
            Console.WriteLine($"账户2: {creditAccount.AccountHolder}, 账户余额: {creditAccount.GetBalance()}元, 信用额度: {creditAccount.CreditLimit}元");

            // 创建ATM
            ATM atm = new ATM();

            // 在ATM存款到普通账号
            atm.DepositToAccount(account1, 500);
            Console.WriteLine($"普通账户余额: {account1.GetBalance()}元");

            // 从ATM取款（普通账号）
            atm.WithdrawFromAccount(account1, 1200);
            Console.WriteLine($"普通账户余额: {account1.GetBalance()}元");

            // 在ATM存款到信用账号
            atm.DepositToAccount(creditAccount, 200);
            Console.WriteLine($"信用账户余额: {creditAccount.GetBalance()}元, 信用额度: {creditAccount.CreditLimit}元");

            // 从ATM取款（信用账号）
            atm.WithdrawFromAccount(creditAccount, 1500);
            Console.WriteLine($"信用账户余额: {creditAccount.GetBalance()}元, 信用额度: {creditAccount.CreditLimit}元");

            // 从ATM取款超过信用额度
            atm.WithdrawFromAccount(creditAccount, 3000);
            Console.WriteLine($"信用账户余额: {creditAccount.GetBalance()}元, 信用额度: {creditAccount.CreditLimit}元");
        }
    }
}